package com.caiyouxi.order.controller;

import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.control.BaseController;
import com.caiyouxi.goods.model.GoodsInfo;
import com.caiyouxi.goods.service.GoodsInfoService;
import com.caiyouxi.order.model.OrderBean;
import com.caiyouxi.order.model.SelectBean;
import com.caiyouxi.order.service.OrderService;
import com.caiyouxi.system.service.SysLogBeanService;
import org.apache.log4j.Logger;
import org.nutz.json.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Created by  on 2015/8/27.
 */
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    private Logger logger = Logger.getLogger(OrderController.class);
    
    @Autowired
    private OrderService orderService;
    @Autowired
    private SysLogBeanService sysLogBeanService;
    @Autowired
    private GoodsInfoService goodsInfoService;

    /**
     * 充值下单
     * @param request
     * @param response
     * @param bean
     * @return
     */
    @RequestMapping("/add")
    public ResData add_order(HttpServletRequest request, HttpServletResponse response, @RequestBody OrderBean bean) {
        GoodsInfo gInfo = goodsInfoService.select_goods_info_by_app_id(bean.getApp_id().toString());
        bean.setGoods_id(Integer.valueOf(gInfo.getId().intValue()));
        OrderBean order = orderService.add_order(bean);
        if(order == null){
            return failed("SYS_ERROR", "系统异常",null);
        }
        //系统日志记录
        sysLogBeanService.add_mess_log(bean.getId() == null ? "" : bean.getId().toString(),"ORDER", bean.getBuyer_user_id() == null ? "" : bean.getBuyer_user_id().toString(),
                Json.toJson(bean),null,"INFO","ORDER",bean.getApp_id() == null ? "" : bean.getApp_id().toString(),"CREATE");
        return success(order);
    }


    /**
     * 买家卖家账户校验
     * currency_code
     * account_code
     * user_id
     */
    @RequestMapping("/check_mess")
    public ResData check_mess(HttpServletRequest request, HttpServletResponse response, @RequestBody OrderBean bean){
        boolean flag = orderService.check_mess(bean);
        if(!flag){
            return failed("SYS_ERROR", "系统异常",null);
        }
        return success(bean);
    }

    /**
     * 结算
     * 根据商品品类的rder_finish_interval自动结算
     */
    @RequestMapping("/order_finish_interval")
    public ResData finish_interval(HttpServletRequest request, HttpServletResponse response, @RequestBody OrderBean bean) {
        orderService.order_finish_interval(bean);
        return success(null);
    }


    /*=============================================================订单相关===========================================================================================*/

    /**
     * 获取订单列表+查询
     * @author wushihao
     * */
    @RequestMapping("/get_orders_list")
    public ResData get_orders_list(@RequestBody OrderBean bean){
        return orderService.get_orders_list(bean);
    }



/**
 * 获取订单详情
 * */
    @RequestMapping("/get_orders_info")
    public ResData get_orders_info(@RequestBody OrderBean bean){
        return orderService.get_orders_info(bean);
    }

    /**
     * 获取订单详情2
     * */
    @RequestMapping("/get_orders_infos")
    public ResData get_orders_infos(@RequestBody SelectBean bean){
        return orderService.get_orders_infos(bean);
    }

    /**
     * 查询优惠券
     * */
    @RequestMapping("/get_coupon_type")
    public ResData get_coupon_type(@RequestBody OrderBean bean){
        return orderService.get_coupon_type(bean);
    }



    /**
     * 撤销订单
     * */
    @RequestMapping("/revoke_order")
    public ResData revoke_order(@RequestBody OrderBean bean){
        return orderService.revoke_order(bean);
    }

    /*=============================================================订单相关结束===========================================================================================*/

}
